﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Willows.Model
{
	public partial class Model 
		: HierarchicalEntity
	{
		private static Model instance;
		public static Model Instance
		{
			get { return Model.instance; }			
		}

		private TypeRegistry typeRegistry;
		public TypeRegistry TypeRegistry
		{
			get { return typeRegistry; }
			protected set { typeRegistry = value; }
		}

		private Dictionary<string, Module> modules;
		public Dictionary<string, Module> Modules
		{
			get { return modules; }
		}

		/// <summary>
		/// The top level module of the design
		/// </summary>
		private Module top;
		public Module Top
		{
			get { return top; }
			set { top = value; }
		}
		
		public Model() 
			: base(new HierarchyRoot(), "Model")
		{
			this.modules = new Dictionary<string, Module>();
			instance = this;
		}

		public void FlattenHierarchy()
		{

		}

		public void Partition()
		{
			ContainerModule hardware = new ContainerModule(top, "hardware");
			hardware.HierarchicalName = "top_hardware";			
			Modules.Add("hardware", hardware);
			top.Children.Add("hardware", hardware);

			ContainerModule software = new ContainerModule(top, "software");
			software.HierarchicalName = "top_software";			
			Modules.Add("software", software);
			top.Children.Add("software", software);

			top.Partition();
		}

		public void SetPartitionChoice(Dictionary<string, PartitionChoice> partition)
		{
			top.SetPartitionChoice(partition);
		}

		#region Write out hierarchy (debug)
		public void WriteChannelHierarchy(System.IO.TextWriter textWriter)
		{
			System.CodeDom.Compiler.IndentedTextWriter indentedTextWriter =
				new System.CodeDom.Compiler.IndentedTextWriter(textWriter);

			top.WriteChannelHierarchy(indentedTextWriter);
		}

		public void WriteHierarchy(System.IO.TextWriter textWriter)
		{
			System.CodeDom.Compiler.IndentedTextWriter indentedTextWriter =
				new System.CodeDom.Compiler.IndentedTextWriter(textWriter);

			top.WriteHierarchy(indentedTextWriter);
		}

		public void WriteChannelHierarchyToFile(string filename)
		{
			System.IO.FileStream file = null;
			System.IO.StreamWriter writer = null;

			try
			{
				file = System.IO.File.Open(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
				writer = new System.IO.StreamWriter(file);

				WriteChannelHierarchy(writer);
			}
			finally
			{
				if (writer != null)
					writer.Close();

				if (file != null)
					file.Close();
			}
		}

		public void WriteHierarchyToFile(string filename)
		{
			System.IO.FileStream file = null;
			System.IO.StreamWriter writer = null;

			try
			{
				file = System.IO.File.Open(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
				writer = new System.IO.StreamWriter(file);

				WriteHierarchy(writer);
			}
			finally
			{
				if (writer != null)
					writer.Close();

				if (file != null)
					file.Close();
			}
		}
		#endregion
	}
}
